****************************************
****************************************
* THIS FILE REPLICATES THE EVIDENCE USING
* THE COMPUSTAT DATA.
****************************************
****************************************

* IMPORTANT: make sure to run start.do before to set up the directories.  


/*------------------------- 
	Organization

		SECTION 1. Descriptives
		
		SECTION 2: Law of motion
		
-------------------------*/ 


/**********************************************************************/
/*  SECTION 1. Descriptives 			
    Notes: 

	1.1 Table 2: Descriptive statistics for Compustat data
	1.2 Figure 11: Sales Share of Top Percentiles of Firms in Compustat
    */
/**********************************************************************/	


 
/*----------------------------------------------------*/
   /* [>   1.1  Table 2: Descriptive statistics for Compustat data   <] */ 
/*----------------------------------------------------*/

tempfile meandescriptives sddescriptives mediandescriptives p25descriptives p75descriptives

use "${data}/MarkupCyclicalityCleaned.dta", clear

preserve
	collapse (mean) sales pctchangesales markup pctchangemarkup
	gen Stats = "Mean"
	order Stats sales pctchangesales markup pctchangemarkup
	save "`meandescriptives'", replace
restore

preserve
	collapse (p25) sales pctchangesales markup pctchangemarkup
	gen Stats = "P 25"
	order Stats sales pctchangesales markup pctchangemarkup
	save "`p25descriptives'", replace
restore

preserve
	collapse (p50) sales pctchangesales markup pctchangemarkup
	gen Stats = "P 50"
	order Stats sales pctchangesales markup pctchangemarkup
	save "`mediandescriptives'", replace
restore

preserve
	collapse (p75) sales pctchangesales markup pctchangemarkup
	gen Stats = "P 75"
	order Stats sales pctchangesales markup pctchangemarkup
	save "`p75descriptives'", replace
restore

preserve
	collapse (sd) sales pctchangesales markup pctchangemarkup
	gen Stats = "SD"
	order Stats sales pctchangesales markup pctchangemarkup
	save "`sddescriptives'", replace
restore

use "`meandescriptives'", clear
append using "`p25descriptives'"
append using "`mediandescriptives'"
append using "`p75descriptives'"
append using "`sddescriptives'"

foreach var of varlist sales pctchangesales markup pctchangemarkup {

	gen temp = strofreal(`var', "%9.2f")
	drop `var'
	rename temp `var'
}

label var Stats "Statistic"
label var sales "Real sales"
label var pctchangesales "% change in sales_{t+1}"
label var markup "Real markup"
label var pctchangemarkup "% change in markup_{t+1}"

texsave Stats sales pctchangesales markup pctchangemarkup ///
	using "${tables}/descriptives.tex", replace hlines(1(1)5)  varlabels location("H") frag ///
	footnote("\emph{Notes:}  The Compustat final dataset contains 242,155 observations for 20,252 firms across 67 years. Variables are in millions") ///
	title("Descriptive statistics for Compustat data")


 
/*----------------------------------------------------*/
   /* [>   1.2  Figure 11: Sales Share of Top Percentiles of Firms in Compustat    <] */ 
/*----------------------------------------------------*/

use "${data}/MarkupCyclicalityCleaned.dta", clear

gsort sic2 year
foreach num of numlist 99 95 80 {

	by sic2: egen p`num' = pctile(sales), p(`num')
	gen pctl`num' = sales >= p`num'
	drop p`num'
}


gen double saletop_1     = sales if pctl99 == 1
gen double saletop_1_5   = sales if pctl95 == 1 & pctl99 == 0
gen double saletop_5_20  = sales if pctl80 == 1 & pctl95 == 0
gen double saletop_20_100 = sales if pctl80 == 0

preserve
	gcollapse (sum) saletop_1 saletop_1_5 saletop_5_20 saletop_20_100 sales, by(timevar)
	replace saletop_1 = (saletop_1/sales)*100
	replace saletop_1_5 = (saletop_1_5/sales)*100
	replace saletop_5_20 = (saletop_5_20/sales)*100
	replace saletop_20_100 = (saletop_20_100/sales)*100


	colorpalette hcl blues, n(35) select(1(5)30) nograph
	graph twoway connected saletop_1 saletop_1_5 saletop_5_20 saletop_20_100 timevar, ///
		graphregion(color(white)) plotregion(color(white)) ///
		ytitle("Share of total sales (%)") xtitle("Year") ///
		legend(label(1 "Top 1%") label(2 "1-5%") label(3 "5-20%") label(4 "20-100%")) ///
		lcolor(`r(p)') lwidth("thin" "thin" "thin" "thin") ///
		msymbol(circle diamond triangle none) msize("vsmall" "vsmall" "vsmall" "small") mcolor(`r(p)') ///
		xlabel(1950(10)2010)
	graph export "${figures}/Sales_distribution_percentiles.pdf", replace

restore


/*------------------------------------ End of SECTION 2 ------------------------------------*/





/**********************************************************************/
/*  SECTION 2: Law of motion
    Notes: 
		2.1 Table 3: Law of motion for the U.S.

		2.2 Table 4: Law of motion for the U.S. split for above and below the 
		30th percentile of lag log markup ratio and negative of lag of debt-to-asset ratio

		2.3 Table 5: Law of motion for the U.S. split for above and below the 
		30th percentile of lag relative sales and negative of lag debt-to-asset ratio


		
	
 */
/**********************************************************************/


use "${data}/MarkupCyclicalityCleaned.dta", clear


 
/*----------------------------------------------------*/
   /* [>   2.1 Table 3: Law of motion for the U.S.   <] */ 
/*----------------------------------------------------*/

gsort gvkey timevar

eststo clear

eststo: reg logmarkup delta_logsales_f1 logmarkup_f1 i.year i.sic2 ///
	if !mi(logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1, logsales_l1, ///
	logsales_l2, logsales_l3, logsales_l4, logmarkup_l1, logmarkup_l2, logmarkup_l3, ///
	logmarkup_l4), cluster(gvkey)

estadd  local yearfixedeffects "Yes"
estadd  local indusfixedeffects "Yes"
estadd  local method "OLS"

eststo: ivreg2 logmarkup i.timevar (delta_logsales_f1 logmarkup_f1 = logsales_l1 ///
	logsales_l2 logsales_l3 logsales_l4 logmarkup_l1 logmarkup_l2 logmarkup_l3 ///
	logmarkup_l4), gmm2s cluster(gvkey)
estadd  local yearfixedeffects "Yes"
estadd  local indusfixedeffects "No"
estadd  local method "IV-GMM"


eststo: ivreg2 logmarkup i.timevar i.sic2 (delta_logsales_f1 logmarkup_f1 = logsales_l1 ///
	logsales_l2 logsales_l3 logsales_l4 logmarkup_l1 logmarkup_l2 logmarkup_l3 ///
	logmarkup_l4), gmm2s cluster(gvkey) partial(i.sic2)

estadd  local yearfixedeffects "Yes"
estadd  local indusfixedeffects "Yes"
estadd  local method "IV-GMM"
 
esttab using "${tables}/Regression_DeltaSales_IVGMM_IVsample.tex", ///
	replace b(%4.3f) tex se star(* .1 ** .05 *** .01) noconstant /// 
	keep(logmarkup_f1 delta_logsales_f1) ///
	coeflabels (logmarkup "\$log(M_{i,t})\$" logmarkup_f1 "\$log(M_{i,t+1})\$" delta_logsales_f1 "\$\Delta log(sal_{i,t+1})\$") ///
	mtitles("\$log(M_{i,t})\$" "\$log(M_{i,t})\$" "\$log(M_{i,t})\$") ///
	scalars("yearfixedeffects Year FE" "indusfixedeffects Industry FEs" "method Method") nonotes r2(%4.3f) obslast compress ///
	title("Law of motion for the U.S.") addnotes("\$*p < 0.10, ** p < 0.05, *** p < 0.01\$; standard errors are clustered at the firm-level.")

eststo clear


 
/*----------------------------------------------------*/
   /* [>   2.2 Table 4: Law of motion for the U.S. split for above and below the 
		30th percentile of lag log markup ratio and negative of lag of debt-to-asset ratio   <] */ 
/*----------------------------------------------------*/

summ sales if !mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
di `r(sum)'
local sumsales = `r(sum)'

summ sales if !(logmarkup_l1 >= p30_logmarkupl1 & ///
	!mi(logmarkup_l1) & debt_asset_l1 <= p70 & !mi(debt_asset_l1)) & ///
	!mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
local sumsales_other = `r(sum)'
local share_other = `sumsales_other'/`sumsales'
local share_other = string(`share_other',"%04.2f")
di `share_other'

summ sales if logmarkup_l1 >= p30_logmarkupl1 & !mi(mktshare_year_sic1_l1) & ///
	debt_asset_l1 <= p70 & !mi(debt_asset_l1) & ///
	!mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
local sumsales_abv = `r(sum)'
local share_abv = `sumsales_abv'/`sumsales'
local share_abv = string(`share_abv',"%04.2f")
di `share_abv'

eststo clear

eststo: ivreg2 logmarkup i.timevar i.sic2 (delta_logsales_f1 logmarkup_f1 = ///
	logsales_l1 logsales_l2 logsales_l3 logsales_l4 ///
 	logmarkup_l1 logmarkup_l2 logmarkup_l3 logmarkup_l4) ///
 	if !(logmarkup_l1 >= p30_logmarkupl1 & !mi(logmarkup_l1) & debt_asset_l1 <= p70 & !mi(debt_asset_l1)), ///
 	gmm2s cluster(gvkey) partial(i.sic2 i.timevar)
estadd  local abvp30 "No"
estadd  local indusfes "Yes"
estadd  local yearfes "Yes"
estadd  local mktshare "`share_other'"

eststo: ivreg2 logmarkup i.timevar i.sic2 (delta_logsales_f1 logmarkup_f1 = ///
	logsales_l1 logsales_l2 logsales_l3 logsales_l4 ///
 	logmarkup_l1 logmarkup_l2 logmarkup_l3 logmarkup_l4) ///
 	if logmarkup_l1 >= p30_logmarkupl1 & !mi(mktshare_year_sic1_l1) & debt_asset_l1 <= p70 & !mi(debt_asset_l1), ///
 	gmm2s cluster(gvkey) partial(i.sic2 i.timevar)
estadd  local abvp30 "Yes"
estadd  local indusfes "Yes"
estadd  local yearfes "Yes"
estadd  local mktshare "`share_abv'"


esttab using "${tables}/Regression_DeltaSales_byJointDistp30LogMarkup_IVGMM.tex", ///
	replace b(%4.3f) tex se star(* .1 ** .05 *** .01) ///
	keep(logmarkup_f1 delta_logsales_f1) ///
	coeflabels (logmarkup "\$log(M_{i,t})\$" logmarkup_f1 "\$log(M_{i,t+1})\$" delta_logsales_f1 "\$\Delta log(sal_{i,t+1})\$") ///
	mtitles("\$log(M_{i,t})\$" "\$log(M_{i,t})\$") ///
	title("Law of motion for the U.S. split for above and below the 30th percentile of lag log markup ratio and negative of lag of debt-to-asset ratio") ///
	scalars("abvp30 Above 30th perc" "indusfes Industry FEs" "yearfes Year FEs" "mktshare Markert share") ///
	nonotes r2(%4.3f) obslast compress

eststo clear


 
/*----------------------------------------------------*/
   /* [>   2.3. Table 5: Law of motion for the U.S. split for above and below the 
		30th percentile of lag relative sales and negative of lag debt-to-asset ratio   <] */ 
/*----------------------------------------------------*/



summ sales if !mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
di `r(sum)'
local sumsales = `r(sum)'
summ sales if !(mktshare_year_sic1_l1 >= p30_rel & ///
	!mi(mktshare_year_sic1_l1) & debt_asset_l1 <= p70 & !mi(debt_asset_l1)) & ///
	!mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
local sumsales_other = `r(sum)'
local share_other = `sumsales_other'/`sumsales'
local share_other = string(`share_other',"%04.2f")
di `share_other'


summ sales if (mktshare_year_sic1_l1 >= p30_rel & ///
	!mi(mktshare_year_sic1_l1) & debt_asset_l1 <= p70 & !mi(debt_asset_l1)) & ///
	!mi(logsales_l1, logsales_l2, logsales_l3, logsales_l4, ///
 	logmarkup_l1, logmarkup_l2, logmarkup_l3, logmarkup_l4, ///
 	logmarkup, timevar, sic2, delta_logsales_f1, logmarkup_f1)
local sumsales_abv = `r(sum)'
local share_abv = `sumsales_abv'/`sumsales'
local share_abv = string(`share_abv',"%04.2f")
di `share_abv'

eststo clear

eststo: ivreg2 logmarkup i.timevar i.sic2 (delta_logsales_f1 logmarkup_f1 = ///
	logsales_l1 logsales_l2 logsales_l3 logsales_l4 ///
 	logmarkup_l1 logmarkup_l2 logmarkup_l3 logmarkup_l4) ///
 	if !(mktshare_year_sic1_l1 >= p30_rel & !mi(mktshare_year_sic1_l1) ///
 	& debt_asset_l1 <= p70 & !mi(debt_asset_l1)), ///
 	gmm2s cluster(gvkey) partial(i.sic2 i.timevar)
estadd  local abvp30 "No"
estadd  local indusfes "Yes"
estadd  local yearfes "Yes"
estadd  local mktshare "`share_other'"

eststo: ivreg2 logmarkup i.timevar i.sic2 (delta_logsales_f1 logmarkup_f1 = ///
	logsales_l1 logsales_l2 logsales_l3 logsales_l4 ///
 	logmarkup_l1 logmarkup_l2 logmarkup_l3 logmarkup_l4) ///
 	if mktshare_year_sic1_l1 >= p30_rel & !mi(mktshare_year_sic1_l1) ///
 	& debt_asset_l1 <= p70 & !mi(debt_asset_l1), ///
 	gmm2s cluster(gvkey) partial(i.sic2 i.timevar)
estadd  local abvp30 "Yes"
estadd  local indusfes "Yes"
estadd  local yearfes "Yes"
estadd  local mktshare "`share_abv'"
 
esttab using "${tables}/Regression_DeltaSales_byJointDistp30_IVGMM.tex", ///
	replace b(%4.3f) tex se star(* .1 ** .05 *** .01) ///
	keep(logmarkup_f1 delta_logsales_f1) ///
	coeflabels (logmarkup "\$log(M_{i,t})\$" logmarkup_f1 "\$log(M_{i,t+1})\$" delta_logsales_f1 "\$\Delta log(sal_{i,t+1})\$") ///
	mtitles("\$log(M_{i,t})\$" "\$log(M_{i,t})\$") ///
	title("Law of motion for the U.S. split for above and below the 30th percentile of lag relative sales and negative of lag debt-to-asset ratio") ///
	scalars("abvp30 Above 30th perc" "indusfes Industry FEs" "yearfes Year FEs" "mktshare Market share") ///
	nonotes r2(%4.3f) obslast compress

eststo clear



/*------------------------------------ End of SECTION 2 ------------------------------------*/

